package com.yunti.business.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableSupport;
import com.yunti.business.vo.req.GetScoreItemReq;
import com.yunti.business.vo.req.ShowStuInfoReq;
import com.yunti.business.vo.req.UpdateScoreReq;
import com.yunti.business.vo.res.OrchestrateRes;
import com.yunti.business.vo.res.OrchestrateWithSubRes;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.yunti.business.domain.BaseScore;
import com.yunti.business.service.IBaseScoreService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 学生成绩Controller
 *
 * @author ruoyi
 * @date 2024-08-22
 */
@RestController
@RequestMapping("/business/score")
public class BaseScoreController extends BaseController {
    @Autowired
    private IBaseScoreService baseScoreService;

    /**
     * 查询学生成绩列表
     */
    @PreAuthorize("@ss.hasPermi('business:score:list')")
    @GetMapping("/list")
    public TableDataInfo list(BaseScore baseScore) {
        startPage();
        List<BaseScore> list = baseScoreService.selectBaseScoreList(baseScore);
        return getDataTable(list);
    }

    /**
     * 导出学生成绩列表
     */
    @PreAuthorize("@ss.hasPermi('business:score:export')")
    @Log(title = "学生成绩", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseScore baseScore) {
        List<BaseScore> list = baseScoreService.selectBaseScoreList(baseScore);
        ExcelUtil<BaseScore> util = new ExcelUtil<BaseScore>(BaseScore.class);
        util.exportExcel(response, list, "学生成绩数据");
    }

    /**
     * 获取学生成绩详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:score:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(baseScoreService.selectBaseScoreById(id));
    }

    /**
     * 新增学生成绩
     */
    @PreAuthorize("@ss.hasPermi('business:score:add')")
    @Log(title = "学生成绩", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseScore baseScore) {
        return toAjax(baseScoreService.insertBaseScore(baseScore));
    }

    /**
     * 修改学生成绩
     */
    @PreAuthorize("@ss.hasPermi('business:score:edit')")
    @Log(title = "学生成绩", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseScore baseScore) {
        return toAjax(baseScoreService.updateBaseScore(baseScore));
    }

    /**
     * 删除学生成绩
     */
    @PreAuthorize("@ss.hasPermi('business:score:remove')")
    @Log(title = "学生成绩", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(baseScoreService.deleteBaseScoreByIds(ids));
    }

    /**
     * 修改学生成绩
     */
    @PreAuthorize("@ss.hasPermi('business:score:query')")
    @GetMapping("/getScoreItem")
    public TableDataInfo getScoreItem(GetScoreItemReq getScoreItemReq) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        return baseScoreService.getScoreItem(getScoreItemReq, pageNum, pageSize);
    }

    /**
     * 修改学生成绩
     */
    @PreAuthorize("@ss.hasPermi('business:score:edit')")
    @PostMapping("/updateStuScore")
    public AjaxResult updateStuScore(@RequestBody UpdateScoreReq updateScoreReq) {
        return baseScoreService.updateStuScore(updateScoreReq);
    }

    /**
     * 修改学生成绩
     */
    @PostMapping("/getSchoolScoreList")
    public AjaxResult getSchoolScoreList(@RequestParam("raceId") Long raceId, @RequestParam("groupId") Long groupId) {
        return baseScoreService.getSchoolScoreList(raceId, groupId);
    }

    /**
     * 修改学生成绩
     */
    @PostMapping("/getStudentScoreList")
    @Cacheable(cacheNames = "studentScoreList", key = "#raceId")  //指定key
    public AjaxResult getStudentScoreList(@RequestParam("raceId") Long raceId) {
        return baseScoreService.getStudentScoreList(raceId);
    }

    /**
     * 导出手记登分表
     */
    @PostMapping("/outputShoujiWord")
    public void outputShoujiWord(HttpServletResponse response, @RequestParam("subId") Long subId) {
        try {
            baseScoreService.outputShoujiWord(response, subId);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @PostMapping("/outputShoujiWord2")
    public void outputShoujiWord2(HttpServletResponse response, @RequestParam("subId") Long subId, @RequestParam("groupId") Long groupId) {
        try {
            baseScoreService.outputShoujiWord2(response, subId,groupId);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 导出手记登分表
     */
    @PostMapping("/outputScoreWord")
    public void outputScoreWord(HttpServletResponse response, @RequestParam("subId") Long subId, @RequestParam("hasName") Boolean hasName, @RequestParam("groupId") Long groupId) {
        try {
            baseScoreService.outputScoreWord(response, subId, hasName,groupId);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    @PostMapping("/outputScoreExcel")
    public void outputScoreExcel(HttpServletResponse response, @RequestParam("subId") Long subId, @RequestParam("groupId") Long groupId) {
        try {
            GetScoreItemReq getScoreItemReq = new GetScoreItemReq();
            getScoreItemReq.setSubId(subId);
            getScoreItemReq.setGroupId(groupId);
            List<OrchestrateWithSubRes> list = (List<OrchestrateWithSubRes>) baseScoreService.getScoreItem(getScoreItemReq, 1, 1000).getRows();
            ExcelUtil<OrchestrateWithSubRes> util = new ExcelUtil<OrchestrateWithSubRes>(OrchestrateWithSubRes.class);
            util.exportExcel(response, list, "学校数据");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @PostMapping("/outputAllScoreExcel")
    public void outputAllScoreExcel(HttpServletResponse response, @RequestParam("groupId") Long groupId) {
        try {
            baseScoreService.outputAllScoreExcel(response, groupId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @PostMapping("/getScoreBigScreen")
    public AjaxResult getScoreBigScreen(@RequestParam("subId") Long subId, @RequestParam("groupId") Long groupId) {
        try {
            return baseScoreService.getScoreBigScreen(subId, groupId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    @PostMapping("/inputScoreExcel")
    public AjaxResult inputScoreExcel(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<OrchestrateWithSubRes> util = new ExcelUtil<OrchestrateWithSubRes>(OrchestrateWithSubRes.class);
        List<OrchestrateWithSubRes> orchestrateWithSubRes = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = baseScoreService.inputScoreExcel(orchestrateWithSubRes, updateSupport, operName);
        return success(message);
    }

    /**
     * 导出手记登分表
     */
    @PostMapping("/stuReady")
    public AjaxResult stuReady(@RequestBody OrchestrateRes orchestrateRes) {
        return baseScoreService.stuReady(orchestrateRes);
//        try {
//            baseScoreService.outputExcel(response);
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }

    }

    @PostMapping("/getReadyStu")
    public AjaxResult getReadyStu() {
        return baseScoreService.getReadyStu();
//        try {
//            baseScoreService.outputExcel(response);
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }

    }

    @PostMapping("/showStuInfo")
    public AjaxResult showStuInfo(@RequestBody ShowStuInfoReq showStuInfoReq) {
        return baseScoreService.showStuScore(showStuInfoReq);
//        try {
//            baseScoreService.outputExcel(response);
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }

    }


    @PostMapping("/getAllLsmpZb")
    public AjaxResult getAllLsmpZb() {
        return baseScoreService.getAllLsmpZb();
    }

    @PostMapping("/getBigScreenData")
    public AjaxResult getBigScreenData(@RequestParam("groupId") Long groupId) {
        return baseScoreService.getBigScreenData(groupId);
    }

}
